package com.yc.testupload.crawler.test;

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.yc.testupload.crawler.test.Question;

/**
 * Word文档生成器类
 */
public class WordGenerator {
    
    /**
     * 将面试题列表生成Word文档
     * @param questions 面试题列表
     * @param outputPath 输出文件路径
     * @throws IOException 文件操作异常
     */
    public void generateWordDocument(List<Question> questions, String outputPath) throws IOException {
        // 创建新的Word文档
        XWPFDocument document = new XWPFDocument();
        
        // 设置文档样式
        XWPFParagraph titleParagraph = document.createParagraph();
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);
        
        XWPFRun titleRun = titleParagraph.createRun();
        titleRun.setText("牛客网面试题汇总");
        titleRun.setBold(true);
        titleRun.setFontSize(18);
        
        // 添加一个空行
        document.createParagraph();
        
        // 遍历面试题列表，添加到文档中
        for (Question question : questions) {
            // 添加题目编号和标题
            XWPFParagraph questionParagraph = document.createParagraph();
            XWPFRun questionRun = questionParagraph.createRun();
            
            String questionText = question.getNumber() + ". " + question.getTitle();
            questionRun.setText(questionText);
            questionRun.setBold(true);
            questionRun.setFontSize(12);
            
            // 添加题目内容
            XWPFParagraph contentParagraph = document.createParagraph();
            XWPFRun contentRun = contentParagraph.createRun();
            contentRun.setText(cleanHtml(question.getContent()));
            contentRun.setFontSize(11);
            
            // 添加答案和解析
            XWPFParagraph answerParagraph = document.createParagraph();
            XWPFRun answerRun = answerParagraph.createRun();
            answerRun.setText("答案及解析：" + cleanHtml(question.getAnswer()));
            answerRun.setFontSize(11);
            answerRun.setColor("0000FF"); // 蓝色字体
            
            // 添加分隔线
            XWPFParagraph separatorParagraph = document.createParagraph();
            XWPFRun separatorRun = separatorParagraph.createRun();
            separatorRun.setText("-------------------------------------------------------------------------------------------------");
            separatorRun.setFontSize(10);
            separatorRun.setColor("CCCCCC");
        }
        
        // 保存文档
        try (FileOutputStream out = new FileOutputStream(outputPath)) {
            document.write(out);
        } finally {
            document.close();
        }
        
        System.out.println("Word文档已生成：" + outputPath);
    }
    
    /**
     * 清理HTML标签，只保留纯文本内容
     * @param html HTML内容
     * @return 纯文本内容
     */
    private String cleanHtml(String html) {
        if (html == null || html.isEmpty()) {
            return "";
        }
        // 简单的HTML清理，移除标签，保留文本内容
        return html.replaceAll("<[^>]*>", "").trim();
    }
}